/**
 * 新建案件需要的service
 * Created by Brant on 2016/12/28.
 */
module.exports = function ($http,httpservice,$q) {
    var caseBarService = {};


    /*案件类别, 案件类型, 审理程序接口*/
    //caseBarService.newXin_1 = function (userId) {
    //    var params={"typeId":userId};
    //    var deferred = $q.defer();
    //  return  $http({
    //        method: 'POST',
    //        url: httpservice.urlWee+'case/findCaseType.do',
    //        data:params,
    //      /*  headers : {'Content-Type':'application/json'}*/
    //      //  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    //        // headers :{'Content-Type': 'application/x-www-form-urlencoded'}
    //    }).then(function successCallback(response) {
    //        deferred.resolve(response.data);
    //    }, function errorCallback(response) {
    //        deferred.reject(response);
    //    });
    //    return deferred.promise;
    //};
    //案件分组
    caseBarService.getCaseCategory = function () {
        //var params={"typeId":userId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.CaseCategoryUrl,
            //data:params,
          /*  headers : {'Content-Type':'application/json'}*/
          //  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            // headers :{'Content-Type': 'application/x-www-form-urlencoded'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };
    //案件类型
    caseBarService.getCaseType = function (ctId) {

        var params={"classificationId":ctId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.CaseBusinessUrl,
            params:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };
    //案件审理程序接口
    caseBarService.getCaseTrial = function (ctId,ccfId) {
        var params={"firLevelId":ctId,"secLevelId":ccfId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.CaseTrialLevelUrl,
            params:params,
            //headers : {'Content-Type':'application/json'}
            //  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };
    //案由接口
    caseBarService.CaseAnyou = function (typeId,parentId,typeFlag) {
        var params={"typeId":typeId,"keyword":parentId,"typeFlag":typeFlag};
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.CaseAnyouUrl,
            data:params,
            //headers : {'Content-Type':'application/json'}
            //  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };


    //案件保存
    caseBarService.CaseSave = function (jsoncabe) {
       //var params={"creatorid":"20","casetypeid":"1","CaseClassificationId":"2","casebusinessid":"1","casecauseid":"1","ClientName":"第一个委托人姓名","CaseCauseName":"案由名称","ProcedureName":"审理程序名称","ConsignorList":{"UserType":"1","Consignor":[{"name":"成功律师所","nameID":18,"form":"2"},{"name":"刘强","nameID":14,"form":"1"},{"name":"成功律师所","nameID":19,"form":"2"},{"name":"刘强","nameID":11,"form":"1"}]},"AllList":{"PartiesList":[{"name":"成功律师所","nameID":16,"form":"2"},{"name":"刘强","nameID":15,"form":"1"},{"name":"成功律师所","nameID":17,"form":"2"},{"name":"刘强","nameID":14,"form":"1"}],"BarristerList":[{"name":"周琦","nameID":56,"type":"1","form":"1"},{"name":"刘强","nameID":54,"type":"3","form":"1"},{"name":"范建强","nameID":55,"type":"5","form":"1"},{"name":"刘强","nameID":53,"type":"5","form":"1"}]}};
        var params=jsoncabe;

      /*  jsoncabe={"creatorid":20,"casetypeid":1,"CaseClassificationId":2,"casebusinessid":1,"casecauseid":10,"ClientName":"userObject","CaseCauseName":"人格权纠纷","ProcedureName":"一审","ConsignorList":{"UserType":"1","Consignor":[{"name":"阳斌","nameID":19,"form":"1"},{"name":"周奇","nameID":69,"form":"1"}]},"AllList":{"PartiesList":[{"name":"刘强","nameID":58,"form":"2"}],"BarristerList":[{"name":"刘强","nameID":57,"form":"2","type":"1"}]}};*/

         var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.CaseSaveUrl,
            data: params,
            //headers : {'Contestr.parseJSON();t-Type':'application/json'}
            //headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };
    //当事人列表接口
    caseBarService.CaseDangshiRenTan = function (biglawuserid,name) {
        var params={"biglawuserid":biglawuserid, "name":name};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.selectZRRCustomerUrl,
            params:params,
            dataType: 'json'
            //headers : {'Content-Type':'application/json'}
            //  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            //headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };
    /*法人列表的接口*/
    caseBarService.CaseFaRenInfo = function (biglawuserid, companyname) {
        var deferred = $q.defer();
        var params = {'biglawuserid': biglawuserid, 'companyname':companyname};
        $http({
            method: 'GET',
            url: httpservice.selectFRCustomerUrl,
            params: params,
            dataType: 'json'
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response.data);
        });
        return deferred.promise;
    };
    //当事人新增添加
    caseBarService.addZRRCustomer = function (biglawuserid,caseZiRanRenModel,sex,phone,birthday,nationality,profession, workplace,address,status,remark) {
        biglawuserid=  parseInt(biglawuserid);
        sex=  parseInt(sex);
        status=  parseInt(status);

        var params = {
            "biglawuserid": biglawuserid,
            "name": caseZiRanRenModel,
            "sex": sex,
            "contactnumber": phone,
            "birthday": birthday,
            "nationality": nationality,
            "profession": profession,
            "workplace": workplace,
            "address": address,
            "status": status,
            "remark": remark
        };
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.addZRRCustomerUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };
    //添加法人
    caseBarService.addFrenCustomer = function (biglawuserid, caseFrenRenModel, address, representative, post, contactname, contactphone, workplace, status, remark) {

        var params = {
            "biglawuserid": biglawuserid,
            "companyname": caseFrenRenModel,
            "address": address,
            "representative": representative,
            "post": post,
            "contactname": contactname,
            "contactphone": contactphone,
            "workplace": workplace,
            "status": status,
            "remark": remark
        };
        return $http(
            {
                url: httpservice.addFRCustomerUrl,
                method: 'POST',
                params: params,
                dataType: 'json',
                headers: {'Content-Type': 'application/json'}
            }
        )
            .then(function (response) {
                return response.data;
            })
            .catch(function (response) {
                console.error('login error', response.status, response.data);
                $("#errormsgId").html("登录失败，失败原因：" + response.status);
            })
            .finally(function () {
                //console.log("finally finished login");
                /*$("#errormsgId").html("网络链接失败！");*/
            });
    };



    //对方当事人接口
    caseBarService.CaseDangshiRen = function (caseId,caseType,caseClientType,adversaryClientName) {
        var params={"caseId":caseId,"caseType":caseType,"caseClientType":caseClientType,"adversaryClientName":adversaryClientName};
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.CaseDangshiRenUrl,
            data:params,//云飞
            //headers : {'Content-Type':'application/json'}
            //  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };
    //律师成员
    caseBarService.CaseLawyerTian = function (keyword) {
        //此处修改了 搜索的参数
        var params={"keyword":keyword};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.CaseZhiTianUrl,
            params:params,
            //headers : {'Content-Type':'application/json'}
            //  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*案件列表查询*/
    caseBarService.selectInfo = function (status,creatorid,pageNum,pageSize) {
       // var params={"pageNum":pageNum,"pageSize":pageSize,"status":status,"creatorid":creatorid};
       var params={"pageNum":pageNum,"pageSize":pageSize,"status":status,"executorId":creatorid};
        var deferred = $q.defer();
         $http({
            // method: 'POST',
            method: 'GET',
           url: httpservice.ObtainCaseListUrl,
            // url:  "http://192.168.0.107:8080/biglaw/case/selectByExample.do",

            params: params,

            //dataType: 'json',
           // headers: {'Content-Type': 'application/x-www-form-urlencoded'}
           /* headers: {'Content-Type': 'application/json'}*/
        }).then(function successCallback(response) {
            //当前页
            // var maxPage = pageNum;
            // if (maxPage === "") {
            //     maxPage = 1;
            // }
            // //总页数
            // var total = response.data.totalPage;
            // var startPage = response.data.beginPage;
            // var endPage = response.data.endPage;
            // var totalCount = response.data.totalCount;
            // if (total <= maxPage) {
            //     maxPage = total;
            // }
            // if (maxPage <= 0) {
            //     maxPage = 1;
            // }
            // var totalPas = {};
            // for (var i = startPage; i <= endPage; i++) {
            //     totalPas[i - 1] = i;
            // }
            // //设置当前页和总页数
            // response.data.totalPage = total;
            // // response.data.nowPage = maxPage + "/" + total;
            // response.data.totalPas = totalPas;
            // response.data.totalCount = totalCount;
            // response.data.currentPage = pageNum;
            // response.data.page = maxPage;

            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //律师成员
    caseBarService.SelectPersonSetInfo = function (id) {
        var params={"userId":id};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            async: true,
            url: httpservice.queryUserInfoUrl+"?userId="+id,
           /* data:params,*/
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };



    return caseBarService;
};
